package cn.unknowpupil.init.configure.mybatisplus;



import cn.unknowpupil.init.domain.dto.LoginUser;
import cn.unknowpupil.init.utils.security.SecurityUtils;
import cn.unknowpupil.init.utils.strings.StringUtils;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;


/**
 * 处理新增和更新的基础数据填充，配合BaseEntity和MyBatisPlusConfig使用。
 *
 * @Classname: MetaHandler
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2020/4/29 11:35
 * @Description:
 * @Version: 1.0
 */
@Component
public class MetaHandler implements MetaObjectHandler {
    private static final Logger logger = LoggerFactory.getLogger(MetaHandler.class);
    public static final String UPDATE_DATE = "updateTime";
    public static final String CREATE_DATE = "createTime";
    public static final String CREATOR = "createBy";
    public static final String EDITOR = "updateBy";
    public static final String VERSION = "version";
    public static final String STATUS = "status";
    public static final String DEL_FLAG = "delFlag";
    /**
     * 功能描述: <br>
     * 〈 新增数据执行〉
     *
     * @auther: w10g
     * @Date: 2020/4/29 19:49
     * @since: 1.0.0
     * @Param: metaObject {@link MetaObject}
     * @return: void
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        Date date = new Date();

        logger.info(" -------------------- start insert fill 插入方法实体填充...  --------------------");
        // 起始版本 3.3.3(推荐)
        this.strictInsertFill(metaObject, CREATE_DATE, () -> new Date(), Date.class);

        this.strictInsertFill(metaObject, VERSION, () -> 0, Integer.class);
        this.strictInsertFill(metaObject, STATUS, () -> "0", String.class);
        this.strictInsertFill(metaObject, DEL_FLAG, () -> 0, Integer.class);
        LoginUser appLoginUser = SecurityUtils.getLoginUser();
        if (appLoginUser != null) {
            String username = appLoginUser.getUsername();
            if (StringUtils.isNotBlank(username)) {
                this.strictInsertFill(metaObject, CREATOR, () -> username, String.class);
            }
        }



    }

    /**
     * 功能描述: <br>
     * 〈 更新数据执行 MetaObject 的构造器是私有的,只提供 forObject 创建MetaObject对象〉
     *
     * @auther: w10g
     * @Date: 2020/4/29 19:49
     * @since: 1.0.0
     * @Param: metaObject {@link MetaObject}
     * @return: null
     */
    @Override
    public void updateFill(MetaObject metaObject) {

        logger.info(" -------------------- start update fill 更新方法实体填充 ...  --------------------");
        this.strictUpdateFill(metaObject, UPDATE_DATE, () ->  new Date(), Date.class);
        LoginUser appLoginUser = SecurityUtils.getLoginUser();
        if (appLoginUser != null) {
            if (appLoginUser.getUsername() != null) {
                this.strictUpdateFill(metaObject, EDITOR, () -> appLoginUser.getUsername(), String.class);

            }
        }
    }

}